# -*- coding: utf-8 -*-

'''
Created on 26/11/2011

@author: Luis M. Ugalde
www.forondarena.net

Copyright © 2011 Luis M. Ugalde
Licensed under the terms of the MIT License
(see LICENSE for details)

This software can't be considered to be even in Alpha state. It still needs a lot of work.

This test module should include classes to test zeromq related issues or other sort of unit tests.
'''


from . import abstractparser

import re
import zmq
import pickle
import uuid


class ParserInTest(abstractparser.AbstractParser):
    '''
    This test class simply logs traffic received from readers.
    It does not send anything to State Stores, and therefore should not block until at least one becomes available (PUSH socket).
    '''
    
    
    def __init__(self, helperfilename = ""):
        '''
        '''
        
        print self.__class__.__name__ + ": instance created"
        
        abstractparser.AbstractParser.__init__(self)


    def parseLines(self):
        '''
        Process a line previously read from a reader.
        Log the line, and possibly the socket it came through.
        '''

        mustexit = 0
        iteration = 0
        idlesockets = 0

        while True:
            iteration += 1
            socks = dict(self.poller.poll(3000))
            if socks:
                idlesockets = 0
                print "{}-{}: Iteration number: [{}]. \n\tInside if socks, the directory has:\n\t\t {}".format(self.__class__.__name__, self.parserbindaddress, iteration, socks)
                if self.readerconnectsocket in socks and socks.get(self.readerconnectsocket) == zmq.POLLIN:
                    print "{}-{}: Iteration number: [{}]. \n\tInside readerconnectsocket. Trying to receive data".format(self.__class__.__name__, self.parserbindaddress, iteration)
                    line = self.readerconnectsocket.recv()
                    print "{}-{}: Iteration number: [{}]. \n\tHandling readerconnectsocket. The line is: \n\t\t{}.".format(self.__class__.__name__, self.parserbindaddress, iteration, line)
                if self.readerconnectnotifysocket in socks and socks.get(self.readerconnectnotifysocket) == zmq.POLLIN:
                    print "{}-{}: Iteration number: [{}]. \n\tHandling readerconnectnotifysocket.".format(self.__class__.__name__, self.parserbindaddress, iteration)
                    line = self.readerconnectnotifysocket.recv()
                    print "{}-{}: Iteration number: [{}]. \n\tHandling readerconnectnotifysocket. The line is: \n\t\t{}.".format(self.__class__.__name__, self.parserbindaddress, iteration, line)
                    if line == 'END':
                        print "{}-{}: Iteration number: [{}]. \n\tHandling readerconnectnotifysocket. Sending END signal. This should not block (PUB)".format(self.__class__.__name__, self.parserbindaddress, iteration)
                        self.parserbindnotifysocket.send('END')
                        mustexit = 1
                        print "{}-{}: Iteration number: [{}]. \n\tSent END signal to {}".format(self.__class__.__name__, self.parserbindaddress, iteration, self.parserbindnotifyaddress)
            else:
                if mustexit > 0:
                    print "{}-{}: Iteration number: [{}].\n\tNo data left at parser for 3 seconds wind end signal. Ending.".format(self.__class__.__name__, self.parserbindaddress, iteration)
                    break
                if idlesockets > 3:
                    print "{}-{}: Iteration number: [{}]. \n\tNo data received from data or notify sockets for 3 seconds.".format(self.__class__.__name__, self.parserbindaddress, iteration)
                idlesockets += 1

        print "{}-{}: Iteration number: [{}]. \n\tWe are exiting, out of the while loop.".format(self.__class__.__name__, self.parserbindaddress, iteration)

        self.closeSources()
        self.closeNotifySources()
        self.closeBindParser()
        self.closeBindNotifyParser()
        self.closeContext()
        self.file.close()


class ParserOutTest(abstractparser.AbstractParser):
    '''
    This test class simply logs traffic received from readers.
    It does not send anything to State Stores, and therefore should not block until at least one becomes available (PUSH socket).
    TODO
    '''
    
    
    def __init__(self, helperfilename = ""):
        '''
        '''
        
        print self.__class__.__name__ + ": instance created"
        
        abstractparser.AbstractParser.__init__(self)


    def parseLines(self):
        '''
        Process a line previously read from a reader.
        Log the line, and possibly the socket it came through.
        '''

        mustexit = 0
        iteration = 0

        while True:
            iteration += 1
            socks = dict(self.poller.poll(3000))
            if socks:
                print "{}-{}: Iteration number: [{}]. \n\tInside if socks, the directory has:\n\t\t {}".format(self.__class__.__name__, self.parserbindaddress, iteration, socks)
                if self.readerconnectsocket in socks and socks.get(self.readerconnectsocket) == zmq.POLLIN:
                    print "{}-{}: Iteration number: [{}]. \n\tInside readerconnectsocket. Trying to receive data".format(self.__class__.__name__, self.parserbindaddress, iteration)
                    line = self.readerconnectsocket.recv()
                    print "{}-{}: Iteration number: [{}]. \n\tHandling readerconnectsocket. The line is: \n\t\t{}.".format(self.__class__.__name__, self.parserbindaddress, iteration, line)
                if self.readerconnectnotifysocket in socks and socks.get(self.readerconnectnotifysocket) == zmq.POLLIN:
                    print "{}-{}: Iteration number: [{}]. \n\tHandling readerconnectnotifysocket.".format(self.__class__.__name__, self.parserbindaddress, iteration)
                    line = self.readerconnectnotifysocket.recv()
                    print "{}-{}: Iteration number: [{}]. \n\tHandling readerconnectnotifysocket. The line is: \n\t\t{}.".format(self.__class__.__name__, self.parserbindaddress, iteration, line)
                    if line == 'END':
                        print "{}-{}: Iteration number: [{}]. \n\tHandling readerconnectnotifysocket. Sending END signal. This should not block (PUB)".format(self.__class__.__name__, self.parserbindaddress, iteration)
                        self.parserbindnotifysocket.send('END')
                        mustexit = 1
                        print "{}-{}: Iteration number: [{}]. \n\tSent END signal to {}".format(self.__class__.__name__, self.parserbindaddress, iteration, self.parserbindnotifyaddress)
            else:
                if mustexit > 0:
                    print "{}-{}: Iteration number: [{}].\n\tNo data left at parser for 3 seconds wind end signal. Ending.".format(self.__class__.__name__, self.parserbindaddress, iteration)
                    break
                print "{}-{}: Iteration number: [{}]. \n\tNo data received from data or notify sockets for 3 seconds. Restarting.".format(self.__class__.__name__, self.parserbindaddress, iteration)
                self.closeSources()
                self.connectSources()
                self.closeNotifySources()
                self.connectNotifySources()
            
        print "{}-{}: Iteration number: [{}]. \n\tWe are exiting, out of the while loop.".format(self.__class__.__name__, self.parserbindaddress, iteration)

        self.closeSources()
        self.closeNotifySources()
        self.closeBindParser()
        self.closeBindNotifyParser()
        self.closeContext()
        self.file.close()
            

if __name__ == '__main__':
    print "Please, do not run this"
